package com.atguigu.myzhxy.controller;

import com.atguigu.myzhxy.pojo.Grade;
import com.atguigu.myzhxy.service.GradeService;
import com.atguigu.myzhxy.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @ApiOperation("查询接口")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades(
            @ApiParam("显示第几页") @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize") Integer pageSize,
            @RequestParam("gradeName") String gradeName
    ) {
        //分页+条件查询
        Page<Grade> page = new Page<>(pageNo, pageSize);
        IPage<Grade> pageRs = gradeService.getGradeByOpr(page, gradeName);

        //封装Result对象，返回
        return Result.ok(pageRs);
    }

    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@RequestBody Grade grade){

        //调用服务层方法进行增加或修改
        //saveOrUpdate方法是IService接口自带，可以不用实现了
        gradeService.saveOrUpdate(grade);
        return Result.ok();
    }
    @GetMapping("/deleteGrade")
    public Result deleteGrade(@RequestBody List<Integer> ids){
        //IService自带批量删除方法
        gradeService.removeByIds(ids);

        return Result.ok();
    }

}
